package com.etop.weibo.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

/**
 *
 * @author jansing
 * lz_jansing@163.com
 */
public class HibernateUtil {
    private static SessionFactory SESSION_FACORY = buildSessionFactory();
    
    private static SessionFactory buildSessionFactory(){
            Configuration cfg = new Configuration().configure();
            ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
            SessionFactory sessionFactory = cfg.buildSessionFactory(serviceRegistry);
            return sessionFactory;
    }
            
    public static final SessionFactory getSessionFactory(){
        if(SESSION_FACORY==null){
            SESSION_FACORY = buildSessionFactory();
        }
        return SESSION_FACORY;
    }
    public static final Session openSession(){
        if(SESSION_FACORY==null){
            SESSION_FACORY = buildSessionFactory();
        }
        return SESSION_FACORY.openSession();
    }
    public static final void close(Session session){
        session.close();
    }
}
